package sf.dsl;

import sf.database.dialect.DBDialect;
import sf.database.dialect.DefaultDBDialect;
import sf.database.jdbc.sql.SQLContext;
import sf.database.jdbc.sql.SQLParameter;
import sf.database.meta.ColumnMapping;
import sf.database.support.DBMS;
import sf.dsl.example.DSLMethod;
import sf.dsl.example.Example;
import sf.dsl.example.ExampleSQL;
import sf.dsl.example.OffsetLimit;
import sf.dsl.example.OrderField;
import sf.dsl.example.ICondition;
import sf.dsl.example.ITable;
import sf.dsl.example.RawValue;
import sf.dsl.example.SQLFlag;
import sf.dsl.example.SimpleField;
import sf.dsl.example.SimpleTable;
import sf.dsl.example.ValueType;
import sf.dsl.example.WrapperSimpleField;
import sf.tools.ArrayUtils;
import sf.tools.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class UpdateDSL {
    public static final String DEFAULT = "DEFAULT";

    protected SQLFlag sqlFlag = new SQLFlag();

    /**
     * 查询条件是否必须添加表别名,默认false
     */
    protected AtomicBoolean mustTableAlias = new AtomicBoolean(false);
    protected SimpleTable updatTable;
    protected List<SimpleTable> linkTables = new LinkedList<>();
    /**
     * from条件信息
     */
    protected List<ICondition> linkConditions = new LinkedList<>();
    /**
     * where条件信息
     */
    protected List<ICondition> whereConditions = new LinkedList<>();

    protected Map<SimpleField[], Object> setMap = new LinkedHashMap<>();

    /**
     * 排序字段
     */
    protected List<OrderField> orderByList = new LinkedList<>();
    /**
     * 查询总数限制
     */
    protected OffsetLimit offsetLimit;

    protected DBDialect dialect = DefaultDBDialect.instance;

    public UpdateDSL(SimpleTable updatTable) {
        this.updatTable = updatTable;
    }

    public UpdateDSL addFlag(SQLFlag.Position position, String flag) {
        this.sqlFlag.addFlag(position, flag);
        return this;
    }

    public UpdateDSL mustTableAlias(boolean mustTableAlias) {
        this.mustTableAlias.set(mustTableAlias);
        return this;
    }

    public UpdateDSL linkTable(SimpleTable... tables) {
        for (SimpleTable t : tables) {
            linkTables.add(t);
        }
        mustTableAlias.set(true);
        return this;
    }

    public UpdateDSL linkConditions(Collection<ICondition> linkConditions) {
        this.linkConditions.addAll(linkConditions);
        mustTableAlias.set(true);
        return this;
    }

    public UpdateDSL linkConditions(ICondition... conditions) {
        if (ArrayUtils.isNotEmpty(conditions)) {
            Collections.addAll(linkConditions, conditions);
        }
        mustTableAlias.set(true);
        return this;
    }

    public UpdateDSL where(Collection<ICondition> whereConditions) {
        this.whereConditions.addAll(whereConditions);
        mustTableAlias.set(true);
        return this;
    }

    public UpdateDSL where(ICondition... conditions) {
        if (ArrayUtils.isNotEmpty(conditions)) {
            Collections.addAll(whereConditions, conditions);
        }
        return this;
    }

    public UpdateDSL set(SimpleField field, Object value) {
        this.setMap.put(new SimpleField[]{field}, value);
        return this;
    }

    public UpdateDSL set(SimpleField[] fields, Object value) {
        this.setMap.put(fields, value);
        return this;
    }

    public UpdateDSL orderByList(List<OrderField> orderByList) {
        this.orderByList = orderByList;
        return this;
    }

    public UpdateDSL offsetLimit(OffsetLimit offsetLimit) {
        this.offsetLimit = offsetLimit;
        return this;
    }

    public UpdateDSL setDialect(DBDialect dialect) {
        this.dialect = dialect;
        return this;
    }

    public SQLContext getSQLContext() {
        StringBuilder sql = new StringBuilder();
        List<SQLParameter> list = new ArrayList<>();
        SQLContext sqlContext = new SQLContext();
        sqlFlag.toSql(SQLFlag.Position.START, sql, list);

        if (DBMS.clickhouse.getNumber() == dialect.getNumber()) {//clickhouse支持
            sql.append("ALTER TABLE ");
        } else {
            sql.append("update ");
        }
        sqlFlag.toSql(SQLFlag.Position.AFTER_SELECT, sql, list);
        if (updatTable != null) {
            boolean addUpdateTable = false;
            if (DBMS.sqlserver.getNumber() == dialect.getNumber()) {
                if (updatTable.hasAlais()) {
                    updatTable.toOnlyAliasSql(sql, list, dialect);
                    addUpdateTable = true;
                }
            }
            if (!addUpdateTable) {
                updatTable.toSql(sql, list, dialect);
            }
        }
        if (DBMS.mysql.getNumber() == dialect.getNumber()) {
            for (ITable s : linkTables) {
                sql.append(',');
                s.toSql(sql, list, dialect);
            }
            for (ICondition condition : linkConditions) {
                condition.toSql(sql, list, mustTableAlias, dialect);
            }
        }
        sqlFlag.toSql(SQLFlag.Position.AFTER_PROJECTION, sql, list);
        if (DBMS.clickhouse.getNumber() == dialect.getNumber()) {//clickhouse支持
            sql.append(" update ");
        } else {
            sql.append(" set ");
        }
        setValues(sql, list);
        if (DBMS.postgresql.getNumber() == dialect.getNumber() ||
                DBMS.sqlserver.getNumber() == dialect.getNumber() ||
                DBMS.sqlite.getNumber() == dialect.getNumber()) {
            if (!linkTables.isEmpty() || !linkConditions.isEmpty()) {
                sql.append(" from ");
            }
            if (!linkTables.isEmpty()) {
                boolean flag = false;
                for (ITable s : linkTables) {
                    if (flag) {
                        sql.append(',');
                    }
                    s.toSql(sql, list, dialect);
                    flag = true;
                }
            } else {
                updatTable.toSql(sql, list, dialect);
            }
            for (ICondition condition : linkConditions) {
                condition.toSql(sql, list, mustTableAlias, dialect);
            }
        }
        StringBuilder temp = new StringBuilder();
        ExampleSQL.getSQLContextWhereClause(temp, list, whereConditions, mustTableAlias, dialect, false);
        String whereClause = temp.toString();
        if (StringUtils.isNotBlank(whereClause)) {
            sql.append(" where ").append(whereClause);
        }

        if (DBMS.mysql.getNumber() == dialect.getNumber() || DBMS.sqlite.getNumber() == dialect.getNumber()) {
            temp.delete(0, temp.length());
            ExampleSQL.getOrderByClause(temp, list, orderByList, mustTableAlias, dialect);
            String orderByCaluse = temp.toString();
            if (StringUtils.isNotBlank(orderByCaluse)) {
                sql.append(" order by ").append(orderByCaluse);
            }
            if (offsetLimit != null) {
                sql = ExampleSQL.buildLimitOffsetSql(sql, dialect, offsetLimit);
            }
        }
        sqlFlag.toSql(SQLFlag.Position.END, sql, list);
        sqlContext.setSql(sql.toString());
        sqlContext.setParas(list);
        return sqlContext;
    }

    private void setValues(StringBuilder sql, List<SQLParameter> list) {
        boolean f = false;
        for (Map.Entry<SimpleField[], Object> entry : setMap.entrySet()) {
            if (f) {
                sql.append(',');
            }
            SimpleField[] fields = entry.getKey();
            boolean useTablePrefix = true;
            if (DBMS.postgresql.getNumber() == dialect.getNumber() || DBMS.sqlite.getNumber() == dialect.getNumber()) {
                useTablePrefix = false;
            }
            ColumnMapping[] cmList = DSLMethod.columnList(sql, list, fields, mustTableAlias, useTablePrefix, dialect);
            sql.append(" = ");
            Object value = entry.getValue();
            setValue(sql, list, cmList, value);
            f = true;
        }
    }

    private void setValue(StringBuilder sql, List<SQLParameter> list, ColumnMapping[] cmList, Object value) {
        if (DEFAULT.equals(value)) {
            sql.append(DEFAULT);
        } else if (value instanceof RawValue) {//直接保存原始值
            sql.append(((RawValue) value).getContext());
        } else if (value instanceof SimpleField) {
            ((SimpleField) value).toConditionSql(sql, list, mustTableAlias, true, dialect);
        } else if (value instanceof ITable) {
            ((ITable) value).toNoAliasSql(sql, list, dialect);
        } else if (value instanceof Example) {
            WrapperSimpleField wsf = new WrapperSimpleField((Example) value);
            wsf.toConditionSql(sql, list, mustTableAlias, true, dialect);
        } else {
            ValueType valueType = ValueType.singleValue;
            int columnSize = cmList.length;
            if (columnSize > 1) {
                if (value instanceof Collection) {
                    valueType = ValueType.listValue;
                } else if (value.getClass().isArray()) {
                    valueType = ValueType.arrayValue;
                }
            }
            DSLMethod.addPlaceholderByValue(sql, columnSize, columnSize, value, valueType);
            DSLMethod.setSqlParameters(list, columnSize, cmList, columnSize, value, valueType);
        }
    }
}
